﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using BBMS.DLL.Base;
using ExcelLibrary.SpreadSheet;
namespace BBMS.DLL.Forms.Bill.Export
{
    public partial class frmExportFilter : FrmFilterBase
    {
        public frmExportFilter()
        {
            InitializeComponent();
            GetMaNCCList();
            GetQuayList();
        }

        private void GetMaNCCList()
        {
            ArrayList arret = NhaCungCapList.getAllMaNCC();
            cbxMaNCC.Items.Add(new KeyValuePair("", "Tất cả"));
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    KeyValuePair kvp = (KeyValuePair)arret[i];
                    cbxMaNCC.Items.Add(kvp);
                }
            }
            cbxMaNCC.SelectedIndex = 0;
        }

        private void GetQuayList()
        {
            ArrayList arret = QuayList.getAllMaQuay();
            cbxQuay.Items.Add(new KeyValuePair("", "Tất cả"));
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    KeyValuePair kvp = (KeyValuePair)arret[i];
                    cbxQuay.Items.Add(kvp);
                }
            }
            cbxQuay.SelectedIndex = 0;
        }
        
        public new static readonly string FunctionKey = "CT_HDX";
        protected override string GetFunctionKey() { return "CT_HDX"; }
        protected override BBMS.DLL.Base.ObjectBase GetObjectBase() { return new HoaDonXuat(); }

        protected override void GenColHeaders()
        {
            lvList.Columns.Clear();
            lvList.Columns.Add("Ký hiệu", 80);
            lvList.Columns[0].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Số hiệu", 80);
            lvList.Columns[1].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Ngày HĐ", 80);
            lvList.Columns[2].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Nhà cung cấp", 200);
            lvList.Columns.Add("Mã Quầy", 80);
            lvList.Columns[4].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Tên Quầy", 150);
            lvList.Columns.Add("Giá trị chưa thuế", 100);
            lvList.Columns[6].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Thuế suất", 80);
            lvList.Columns[7].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Giá trị thuế", 100);
            lvList.Columns[8].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Giá trị HĐ", 100);
            lvList.Columns[9].TextAlign = HorizontalAlignment.Right;
        }
        protected override ArrayList GenSampleData()
        {
            ArrayList ar = new ArrayList();
            ar.Add(new string[] { "STT", "Mã chứng từ", "Số chứng từ", "Ngày chứng từ", "Hạn thanh toán",
            "Ngày thanh toán dự kiến" , "Ngày thanh toán", "Trạng thái thanh toán", "Mã hồ sơ",
            "Mã NCC", "Mã nhà cung cấp", "Giá trị chưa thuế","Thuế suất", "Giá trị thuế", "Giá trị chứng từ", "Giá trị đã thanh toán",
            "Ghi chú" ,"Mã nhân viên"});
            HoaDonXuat hoaDonXuatList = new HoaDonXuat();
            ArrayList arr = hoaDonXuatList.GetAllObject(fParams);
            if (arr != null)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    HoaDonXuat hdx = (HoaDonXuat)arr[i];
                    ar.Add(new string[] { hdx.ID.ToString(), hdx.MaCT, hdx.SoCT, hdx.NgayCT.ToString(), hdx.NgayHT.ToString()
                    , hdx.NgayTTDuKien.ToString(),hdx.NgayTT.ToString(), ((int)hdx.TrangThaiTT).ToString()
                    , hdx.MaHoSo, hdx.MaNCC, hdx.MaQuay, hdx.GiaTriChuaThue.ToString(),
                    hdx.ThueSuat.ToString(),hdx.GiaTriThue.ToString(),hdx.GiaTriCT.ToString(),
                    hdx.GiaTriDaTT.ToString(),hdx.GhiChu.ToString(),hdx.NVID.ToString()});                    
                }
            }
            //ArrayList ar = new ArrayList();
            ////ar.Add(new string[] { "Mã NCC", "Tên NCC", "Địa chỉ", "Thành phố", });
            ////ar.Add(new string[] { "QTP", "Quầy thực phẩm", "Food counter", "NTP" });
            //// define col width
            ////ar.Add(new int[] { 100, 300, 100, 300 });
            return ar;
        }

        protected override ArrayList GetImportedList(Worksheet sheet)
        {
            ArrayList arret = new ArrayList();
            for (int i = 1; i < sheet.Cells.Rows.Count; i++)
            {
                HoaDonXuat hdx = new HoaDonXuat();
                Cell cell = sheet.Cells[i, 0];
                if (cell.Value.ToString() != "") hdx.ID = Convert.ToInt32(cell.Value.ToString());
                cell = sheet.Cells[i, 1];
                hdx.MaCT = cell.Value.ToString();
                cell = sheet.Cells[i, 2];
                hdx.SoCT = cell.Value.ToString();
                cell = sheet.Cells[i, 3];
                if (cell.Value.ToString() != "") hdx.NgayCT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 4];
                if (cell.Value.ToString() != "") hdx.NgayHT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 5];
                if (cell.Value.ToString() != "") hdx.NgayTTDuKien = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 6];
                if (cell.Value.ToString() != "") hdx.NgayTT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 7];
                foreach (TrangThaiThanhToan item in Enum.GetValues(typeof(TrangThaiThanhToan)))
                {
                    if (item.ToString() == cell.Value.ToString())
                        hdx.TrangThaiTT = item;
                }

                cell = sheet.Cells[i, 8];
                hdx.MaHoSo = cell.Value.ToString();
                cell = sheet.Cells[i, 9];
                hdx.MaNCC = cell.Value.ToString();
                cell = sheet.Cells[i, 10];
                hdx.MaQuay = cell.Value.ToString();
                cell = sheet.Cells[i, 11];
                if (cell.Value.ToString() != "") hdx.GiaTriChuaThue = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 12];
                if (cell.Value.ToString() != "") hdx.ThueSuat = Convert.ToInt32(cell.Value.ToString());
                cell = sheet.Cells[i, 13];
                if (cell.Value.ToString() != "") hdx.GiaTriThue = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 14];
                if (cell.Value.ToString() != "") hdx.GiaTriCT = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 15];
                if (cell.Value.ToString() != "") hdx.GiaTriDaTT = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 16];
                hdx.GhiChu = cell.Value.ToString();
                cell = sheet.Cells[i, 17];
                if (cell.Value.ToString() != "") hdx.NVID = Convert.ToInt32(cell.Value.ToString());
                arret.Add(hdx);

            }
            return arret;
        }

        protected override void UpdateFilterParams()
        {
            if (txtMa.Text.Trim() != "" || txtSoCT.Text.Trim() != "" || cbxMaNCC.SelectedIndex != -1 || cbxQuay.SelectedIndex != -1
               || txtGiaTriCTTu.Text.Trim() != "0" || txtGiaTriCTDen.Text.Trim() != "0")
            {
                fParams = new FilterParams();
                //FilterCondition fcLoaiCTCK = new FilterCondition("LoaiCT", "LoaiCT", "HDX", SQLOperator.EQUAL);
                //fParams.ArParams.Add(fcLoaiCTCK);
                //FilterCondition fcIsChi = new FilterCondition("IsChi", "IsChi", false, SQLOperator.EQUAL);
                //fParams.ArParams.Add(fcIsChi);
                if (txtMa.Text.Trim() != "")
                {
                    FilterCondition fcMa = new FilterCondition("MaCT", "MaCT", "%" + txtMa.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcMa);
                }

                if (txtSoCT.Text.Trim() != "")
                {
                    FilterCondition fcSoCT = new FilterCondition("SoCT", "SoCT", "%" + txtSoCT.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcSoCT);
                }

                if (cbxMaNCC.SelectedIndex != 0)
                {
                    FilterCondition fcMaNCC = new FilterCondition("MaNCC", "MaNCC", ((KeyValuePair)cbxMaNCC.SelectedItem).Key, SQLOperator.EQUAL);
                    fParams.ArParams.Add(fcMaNCC);
                }

                if (cbxQuay.SelectedIndex != 0)
                {
                    FilterCondition fcMaQuay = new FilterCondition("MaQuay", "MaQuay", ((KeyValuePair)cbxQuay.SelectedItem).Key, SQLOperator.EQUAL);
                    fParams.ArParams.Add(fcMaQuay);
                }

                if (txtGiaTriCTTu.Text.Trim() != "" && txtGiaTriCTTu.Text.Trim() != "0")
                {
                    FilterCondition fcGiaTriCTTu = new FilterCondition("GiaTriCT", "GiaTriCT", txtGiaTriCTTu.Text.Trim().Replace(",", ""), SQLOperator.GREATER_OR_EQUAL);
                    fParams.ArParams.Add(fcGiaTriCTTu);
                }

                if (txtGiaTriCTDen.Text.Trim() != "" && txtGiaTriCTDen.Text.Trim() != "0")
                {
                    FilterCondition fcGiaTriCTDen = new FilterCondition("GiaTriCT", "GiaTriCT", txtGiaTriCTDen.Text.Trim().Replace(",", ""), SQLOperator.LOWER_OR_EQUAL);
                    fParams.ArParams.Add(fcGiaTriCTDen);
                }


                fParams.CurrentPage = 0;
            }
            else
            {
                MessageBox.Show(null, "Bạn phải nhập nội dung tìm kiếm", "Thông báo");
            }

        }

        protected override FrmEditBase CreateEditForm(BBMS.DLL.Base.ObjectBase obj)
        {
            return new frmExportUpdate(obj);
        }

        protected override void AddNewObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            if (obj == null) return;
            HoaDonXuat hdx = (HoaDonXuat)obj;
            ListViewItem it = new ListViewItem(hdx.MaCT);
            it.SubItems.Add(hdx.SoCT);
            it.SubItems.Add(hdx.NgayCT.ToString("dd/MM/yyyy"));
            it.SubItems.Add(ObjectLists.NhaCungCapList.getObjectName(hdx.MaNCC));
            it.SubItems.Add(hdx.MaQuay);
            it.SubItems.Add(ObjectLists.QuayList.getObjectName(hdx.MaQuay));
            it.SubItems.Add(CommonTools.Helper.MoneyString(hdx.GiaTriChuaThue));
            it.SubItems.Add(CommonTools.Helper.PercentString(hdx.ThueSuat));
            it.SubItems.Add(CommonTools.Helper.MoneyString(hdx.GiaTriThue));
            it.SubItems.Add(CommonTools.Helper.MoneyString(hdx.GiaTriCT));
            it.Tag = obj;
            lvList.Items.Add(it);
        }

        protected override void UpdateObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            if (obj == null) return;
            foreach (ListViewItem it in lvList.Items)
            {
                HoaDonXuat hdx = (HoaDonXuat)obj;
                if (((HoaDonXuat)it.Tag).MaCT == hdx.MaCT)
                {
                    it.SubItems[2].Text = hdx.NgayCT.ToString("dd/MM/yyyy");
                    it.SubItems[3].Text = ObjectLists.NhaCungCapList.getObjectName(hdx.MaNCC);
                    it.SubItems[4].Text = hdx.MaQuay;
                    it.SubItems[5].Text = ObjectLists.QuayList.getObjectName(hdx.MaQuay);
                    it.SubItems[6].Text = CommonTools.Helper.MoneyString(hdx.GiaTriChuaThue);
                    it.SubItems[7].Text = CommonTools.Helper.PercentString(hdx.ThueSuat);
                    it.SubItems[8].Text = CommonTools.Helper.MoneyString(hdx.GiaTriThue);
                    it.SubItems[9].Text = CommonTools.Helper.MoneyString(hdx.GiaTriCT);
                    it.Tag = obj;
                    break;
                }
            }
        }
    }
}
